<template>
  <el-dialog :title="title" :visible.sync="visible" width="600px" :before-close="handleClose">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px">
      <el-form-item label="课目名称：" prop="projectName">
        <el-input v-model.trim="ruleForm.projectName" placeholder="请输入" clearable class="input-w300"
          maxlength="100"></el-input>
      </el-form-item>
      <el-form-item label="简介：" prop="projectDesc">
        <el-input v-model.trim="ruleForm.projectDesc" type="textarea" placeholder="请输入" clearable class="input-w300"
          maxlength="200" show-word-limit></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleConfirm('ruleForm')">确 定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addTrainingProject, updateTrainingProject, queryTrainingSubjectDetail } from "@/api/subject";

export default {
  components: {},
  dicts: [],
  props: {
    visible: {
      default: false,
      type: Boolean
    },
    title: {
      default: '',
      type: String
    },
    editType: {
      default: 'add',
      type: String
    },
    editData: {
      default: {},
      type: Object
    }
  },
  data () {
    return {
      ruleForm: {
        projectName: '',
        projectDesc: "",
      },
      rules: {
        projectName: [
          { required: true, message: '请输入课目名称', trigger: 'blur' },
        ]
      }

    };
  },
  watch: {
    visible (val) {
      if (val) {
        if (this.editType === 'edit') {
          this.ruleForm = JSON.parse(JSON.stringify(this.editData))
        }
      } else {
        this.ruleForm = {
          projectName: '',
          projectDesc: "",
        }
        this.resetForm('ruleForm')
      }
    }
  },
  methods: {
    resetForm (formName) {
      this.$refs[formName].resetFields();
    },
    // 确定
    handleConfirm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await (this.ruleForm.id ? updateTrainingProject(this.ruleForm) : addTrainingProject(this.ruleForm))
          if (res.code === 200) {
            this.$message.success('保存成功');
            this.handleClose()
          }
        }
      });
    },
    // 取消
    handleCancel () {
      this.$emit('update:visible', false)
    },
    // 关闭
    handleClose () {
      this.$emit('update:visible', false)
      this.$emit('close')
    }
  }
};
</script>
<style lang='scss' scoped></style>
